import { API_URL } from "../constants";
import type { IOrder, IResponse, IResponseList } from "../types";
import request from "./request";

type GetOrderParams = {
  orderNo?: string;
  orderStatus?: string;
  pageNum?: number;
  pageSize?: number;
};
export function getOrderList(
  params: GetOrderParams,
  token: string,
  signal?: AbortSignal,
) {
  return request<IResponseList<{ rows: IOrder[] }>>(
    `${API_URL}/system/order/list`,
    {
      headers: { Authorization: token },
      params: params as any,
      signal,
    },
  );
}

type AddOrderData = {
  deviceId: number;
  modelName: string;
  modelNo: string; // 型号
  orderNo: string; // 委托单号
  experimentItem: string; // 实验项目
  programId: number;
};
export function addOrder(data: AddOrderData, token: string) {
  return request<IResponse>(`${API_URL}/system/order`, {
    method: "post",
    data,
    headers: { Authorization: token },
  });
}

type UpdateOrderData = AddOrderData & {
  orderId: number;
};
export function updateOrder(data: UpdateOrderData, token: string) {
  return request<IResponse>(`${API_URL}/system/order`, {
    method: "put",
    data,
    headers: { Authorization: token },
  });
}

export function runOrder(orderId: number, token: string) {
  return request<IResponse>(`${API_URL}/system/order/run`, {
    params: { orderId } as any,
    headers: { Authorization: token },
  });
}

export function getOrderDetail(orderId: number, token: string) {
  return request<IResponse<{ data: IOrder }>>(
    `${API_URL}/system/order/${orderId}`,
    {
      headers: { Authorization: token },
    },
  );
}
